package Controller

import (
	_ "fmt"
	"github.com/gin-gonic/gin"
	_ "log"
	"net/http"
	"repair/Database"
	"repair/Table_Struct"
	"repair/util"
	_ "strconv"
)
//客户信息录入
func Customerservice_enter(c *gin.Context) {
	db := Database.GetDB()
	//获取参数
	cname := c.PostForm("cname")
	ctelephone := c.PostForm("ctelephone")
	cpassword := c.PostForm("cpassword")
	cid := c.PostForm("cid")
	ctel := c.PostForm("ctel")
	cemail := c.PostForm("cemail")
	caddress := c.PostForm("caddress")
	cpostcode := c.PostForm("cpostcode")
	ccompany := c.PostForm("ccompany")
	cnature := c.PostForm("cnature")
	//数据验证
	//b, _ := c.GetRawData()
	//var m map[string]string
	//_ = json.Unmarshal(b, &m)
	//cname := m["Cname"]
	//ctelephone := m["Ctelephone"]
	//cpassword := m["Cpassword"]
	//cid := m["Cid"]
	//ctel := m["Ctel"]
	//cemail := m["Cemail"]
	//caddress := m["Caddress"]
	//cpostcode := m["Cpostcode"]
	//ccompany := m["Company"]
	//cnature := m["Cnature"]
	if len(cid) != 18 {
		c.JSON(http.StatusOK, gin.H{"code": 422, "msg": "身份证号必须18位"})
		return
	}
	if len(cpassword) < 6 {
		c.JSON(http.StatusOK, gin.H{"code": 422, "msg": "密码不能少于6位"})
		return
	}
	if len(ctelephone) != 11{
		c.JSON(http.StatusOK, gin.H{"code":422, "msg":"手机号不能少于11位"})
	}
	if len(cemail) == 0{
		c.JSON(http.StatusOK, gin.H{"code":422, "msg":"邮编不能为空"})
	}
	if len(caddress) == 0{
		c.JSON(http.StatusOK, gin.H{"code":422, "msg":"地址不能为空"})
	}
	if len(cpostcode) == 0{
		c.JSON(http.StatusOK, gin.H{"code":422,"msg":"邮编不能为空"})
	}
	//如果名称没有传，给一个10位的随机字符串
	if len(cname) == 0 {
		cname = util.RandomString(10)
	}
	//判断手机号是否存在
	if iscidExist(db, cid) {
		c.JSON(http.StatusOK, gin.H{"code": 422, "msg": "用户已经存在"})
		return
	}
	//创建用户
	newcustomer := Table_Struct.Customer{
		Cname:      cname,
		Ctelephone: ctelephone,
		Cpassword:  cpassword,
		Cid: cid,
		Ctel: ctel,
		Cemail: cemail,
		Caddress: caddress,
		Cpostcode: cpostcode,
		Ccompany: ccompany,
		Cnature: cnature,
	}
	db.Create(&newcustomer)
	//返回结果
	c.JSON(200, gin.H{
		"msg": "录入成功",
	})
}
//客户信息的编辑
func Customerservice_modify(c *gin.Context) {
	db := Database.GetDB()
	cname := c.PostForm("cname")
	ctelephone := c.PostForm("ctelephone")
	cpassword := c.PostForm("cpassword")
	cid := c.PostForm("cid")
	ctel := c.PostForm("ctel")
	cemail := c.PostForm("cemail")
	caddress := c.PostForm("caddress")
	cpostcode := c.PostForm("cpostcode")
	ccompany := c.PostForm("ccompany")
	cnature := c.PostForm("cnature")
	if len(ctelephone) != 11{
		c.JSON(http.StatusOK, gin.H{"code":422, "msg":"手机号不能少于11位"})
	}
	if len(cemail) == 0{
		c.JSON(http.StatusOK, gin.H{"code":422, "msg":"邮编不能为空"})
	}
	if len(caddress) == 0{
		c.JSON(http.StatusOK, gin.H{"code":422, "msg":"地址不能为空"})
	}
	if len(cpostcode) == 0{
		c.JSON(http.StatusOK, gin.H{"code":422,"msg":"邮编不能为空"})
	}
	mcustomer := Table_Struct.Customer{
		Cname: cname,
		Ctelephone: ctelephone,
		Cpassword: cpassword,
		Cid: cid,
		Ctel: ctel,
		Cemail: cemail,
		Ccompany: ccompany,
		Cpostcode: cpostcode,
		Cnature: cnature,
		Caddress: caddress,
	}
	var user Table_Struct.Customer
	db.Where("Cid = ?", cid).Delete(&user)
	db.Create(&mcustomer)
	c.JSON(200, gin.H{
		"msg": "修改成功",
	})
}
//客户信息的删除
func Customer_delete(c *gin.Context) {
	db := Database.GetDB()
	cid := c.Query("cid")
	var user Table_Struct.Customer
	db.Where("Cid = ?", cid).First(&user)
	if user.Cname == "" {
		c.JSON(200, gin.H{
			"code":422,
			"msg" : "无此人",
		})
		return
	} else {
		db.Where("Cid = ?", cid).Delete(&user)
		c.JSON(200,gin.H{"code":200,"msg":"删除成功"})
	}
}
//客户信息的查看
//返回整个库存信息
func Customer_inquire(c *gin.Context) {
	db := Database.GetDB()
	var user[] Table_Struct.Customer
	err := db.Find(&user).Error
	if err!=nil{
		c.JSON(200,gin.H{"error":err.Error()})
	}
	//.Where("Sname = ?", sname).First(&spare)
	c.JSON(200,user)
}
//查询返回单个库存信息
func Customer_inquire_single(c *gin.Context){
	db := Database.GetDB()
	cid := c.PostForm("cid")
	var user Table_Struct.Customer
	db.Where("Cid = ?", cid).First(&user)
	if len(user.Cid) == 0 {
		c.JSON(200,gin.H{"code":422,"msg":"系统中无该备件"})
		return
	}
	c.JSON(200,gin.H{"code":200,"msg":"查询成功"})
	c.JSON(200,user)
}



//维护报修信息
//客服进行报修设备的维护
func Repair_information(c *gin.Context) {
	db := Database.GetDB()
	rnumber := c.PostForm("number")
	rphenomenon := c.PostForm("phenomenon")
	rfault := c.PostForm("fault")
	rproduct := c.PostForm("product")
	rremarks := c.PostForm("remarks")
	rid := c.PostForm("rid")
	var user Table_Struct.Repair_table
	db.Where("Rnumber = ?", rnumber).First(&user)
	if len(user.Rnumber)==0 {
		c.JSON(200,gin.H{"code":400, "msg":"该订单不存在"})
		return
	}
	Ctime := util.DeleteTailBlank(user.Rtime)

	rorder := Table_Struct.Repair_table{
		Rnumber: rnumber,
		Rtime: Ctime,
		Rphenomenon: rphenomenon,
		Rfault: rfault,
		Rproduct: rproduct,
		Rremarks: rremarks,
		Rid: rid,
	}
	var order Table_Struct.Repair_table
	db.Where("Rnumber = ?", rnumber).Delete(&order)
	db.Create(&rorder)
	c.JSON(200, gin.H{
		"msg": "修改成功",
	})
}
//返回客户确认单
func Confirm_repair(c *gin.Context) {
	db := Database.GetDB()
	cid := c.PostForm("cid")
	var user Table_Struct.Customer
	db.Where("Cid = ?",cid).First(&user)
	var repair Table_Struct.Repair_table
	db.Where("Rid = ?",cid).First(&repair)
	type confirm struct {
		time string
		number string
		rphenomenon string
		rfault string
		rproduct string
		caddress string
		ctelephone string
	}
	comfirmrepair := confirm{time:repair.Rtime,number:repair.Rnumber,rphenomenon: repair.Rphenomenon,rfault: repair.Rfault,rproduct: repair.Rproduct,caddress: user.Caddress,ctelephone: user.Caddress}
	c.JSON(200,comfirmrepair)
}
